package com.changgou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@CanalEventListener //声明当前的类是canal的监听类
public class SysDataListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     *
     * @param eventType 当前操作数据库的类型
     * @param rowData 当前操作数据库的数据
     */
    @ListenPoint(schema = "changgou_config",table = "sys_dict_data")
    public void dataUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){

        String code = "";
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if ("dict_code".equals(column.getName())){
                code = column.getValue();
                break;
            }
        }
        //发送消息
        rabbitTemplate.convertAndSend("", RabbitMQConfig.SYS_DATA,code);
    }
    /**
     *
     * @param eventType 当前操作数据库的类型
     * @param rowData 当前操作数据库的数据
     */
    @ListenPoint(schema = "changgou_config",table = "sys_dict_type")
    public void typeUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){

        String id = "";
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if ("dict_id".equals(column.getName())){
                id = column.getValue();
                break;
            }
        }
        //发送消息
        rabbitTemplate.convertAndSend("", RabbitMQConfig.SYS_TYPE,id);
    }
}
